package com.prj.ufdm.web.config;

import java.util.concurrent.Executor;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import javax.annotation.Resource;
import org.springframework.core.env.Environment;

/**
 * 多线程执行配置（通过 @Async 注解来声明一个异步任务）
 * @author Administrator
 */
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
	
	@Resource
    private Environment env;
	
	@Override
	public Executor getAsyncExecutor() {
		
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		int corePoolSize = 8;
		if(env!=null) {
			try {
				corePoolSize = Integer.parseInt(env.getProperty("check.thread.size"));
			}
			catch(Exception er) {
				
			}
		}
		//线程池维护线程的最少数量  
		taskExecutor.setCorePoolSize(corePoolSize);
		//线程池所使用的缓冲队列
		taskExecutor.setQueueCapacity(150);
		//线程池维护线程的最大数量  
		taskExecutor.setMaxPoolSize(1000);
		//线程池维护线程所允许的空闲时间
		taskExecutor.setKeepAliveSeconds(30000);
		taskExecutor.initialize();
		return taskExecutor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return null;
	}

}
